﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

namespace Jy123.Web.Module.Venues
{
    public partial class ModuleRank_List : System.Web.UI.Page
    {
        private Jy123.Model.User_Main user;
        protected void Page_Load(object sender, EventArgs e)
        {
            BindList();
        }

        private void BindList()
        {
            string type = Request.QueryString["Type"];
            string strVId = Request.QueryString["ModuleId"];
            string order = "ID ASC";
            string sort = Request.QueryString["Sort"];
            string sortType = Request.QueryString["SortType"];
            if (!string.IsNullOrEmpty(sort) && !string.IsNullOrEmpty(sortType))
            {
                order = sort + " " + sortType;
            }
            if (!string.IsNullOrEmpty(strVId))
            {
                int moduleId = int.Parse(strVId);
                Jy123.BLL.Module_Main BLLModule = new Jy123.BLL.Module_Main();
                Jy123.Model.Module_Main module = BLLModule.GetModel(moduleId);
                Jy123.BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
                Jy123.Model.Public_JType pJType = BLLJType.GetModel(module.FJType.Value);
                int fJType = pJType.JFather.Value;
                bool isFromClub = false;

                if (fJType == Convert.ToInt32(Jy123.BLL.Public.EnumConfig.JyType.Club_Main))
                {
                    isFromClub = true;
                }

                Jy123.BLL.User_Main BLLUser = new Jy123.BLL.User_Main();
                user = BLLUser.GetModel(module.FUser.Value);
                string html = string.Empty;
                int cJType = module.FJType.Value;
                switch (type)
                {
                    case "Club":
                        BLL.User_Module.User_Club BLLCModule = new BLL.User_Module.User_Club();
                        if (!isFromClub)
                        {
                            cJType = module.FJType.Value + 1;//俱乐部的JType是赛场的JType＋1
                        }
                        List<Jy123.Model.User_Module.User_Club> listClub = BLLCModule.GetFriendModelList(cJType, order, user.ID);
                        html = BindDetail(listClub);

                        break;
                    case "Venues":
                        BLL.User_Module.User_Venues BLLVModule = new Jy123.BLL.User_Module.User_Venues();
                        if (isFromClub)
                        {
                            cJType = module.FJType.Value - 1;//俱乐部的JType是赛场的JType＋1
                        }
                        List<Jy123.Model.User_Module.User_Venues> listVenues = BLLVModule.GetFriendModelList(cJType, order, user.ID);
                        html = BindDetail(listVenues);
                        break;
                    case "TCenter":
                        BLL.User_Module.User_TCenter BLLTModule = new Jy123.BLL.User_Module.User_TCenter();
                        order = order.Replace("RaceTimes", "UsingTimes");
                        List<Jy123.Model.User_Module.User_TCenter> listTCenter = BLLTModule.GetFriendModelList(module.FJType.Value, order, user.ID);
                        html = BindDetail(listTCenter);
                        break;
                }
                Response.Write(html);
            }
        }

        private string BindDetail(List<Jy123.Model.User_Module.User_Club> listClub)
        {
            string html = Cache["moduleranklist"].ToString();
            StringBuilder sbList = new StringBuilder();
            foreach (Jy123.Model.User_Module.User_Club club in listClub)
            {
                string strUl = Cache["modulerankul"].ToString();
                string statusImg = string.Empty;
                string strAlt = string.Empty;
                switch (club.CStatus.Value)
                {
                    case 0:
                        statusImg = "/Images/Module/empty.gif";
                        strAlt = "空闲";
                        break;
                    case 1:
                        statusImg = "/Images/Module/wait.gif";
                        strAlt = "等待";
                        break;
                    case 2:
                        statusImg = "/Images/Module/race.gif";
                        strAlt = "比赛";
                        break;
                    case 3:
                        statusImg = "/Images/Module/training.gif";
                        strAlt = "训练";
                        break;
                    case 4:
                        statusImg = "/Images/Module/hire.gif";
                        strAlt = "租用";
                        break;
                }
                strUl = strUl.Replace("$ClubLogo$", club.Logo);
                string strCName = club.CName;
                if (strCName.Length > 5)
                {
                    strCName = strCName.Substring(0, 5) + "...";
                }
                strUl = strUl.Replace("$ClubName$", strCName);
                strUl = strUl.Replace("$ClubAssets$", (Convert.ToInt32(club.Assets.Value)).ToString());
                strUl = strUl.Replace("$Cash$", (Convert.ToInt32(club.Cash.Value)).ToString());
                strUl = strUl.Replace("$RaceTimes$", club.RaceTimes.ToString());
                strUl = strUl.Replace("$Rate$", club.Rate.ToString() + "%25");
                strUl = strUl.Replace("$StatusImg$", statusImg);
                strUl = strUl.Replace("$Alt$", strAlt);
                strUl = strUl.Replace("$style$", "display:block");
                sbList.Append(strUl);
            }
            html = html.Replace("$ul$", sbList.ToString());
            html = html.Replace("$style$", "display:block");
            html = html.Replace("$Times$", "比赛次数");
            return html;
        }

        private string BindDetail(List<Jy123.Model.User_Module.User_Venues> listVenues)
        {
            string html = Cache["moduleranklist"].ToString();
            StringBuilder sbList = new StringBuilder();
            foreach (Jy123.Model.User_Module.User_Venues module in listVenues)
            {
                string strUl = Cache["modulerankul"].ToString();
                string statusImg = string.Empty;
                string strAlt = string.Empty;
                switch (module.VStatus)
                {
                    case 0:
                        statusImg = "/Images/Module/empty.gif";
                        strAlt = "空闲";
                        break;
                    case 1:
                        statusImg = "/Images/Module/wait.gif";
                        strAlt = "等待";
                        break;
                    case 2:
                        statusImg = "/Images/Module/race.gif";
                        strAlt = "比赛";
                        break;
                    case 4:
                        statusImg = "/Images/Module/hire.gif";
                        strAlt = "租用";
                        break;
                }
                strUl = strUl.Replace("$ClubLogo$", module.Logo);
                string strCName = module.VName;
                if (strCName.Length > 5)
                {
                    strCName = strCName.Substring(0, 5) + "...";
                }
                strUl = strUl.Replace("$ClubName$", strCName);
                strUl = strUl.Replace("$ClubAssets$", (Convert.ToInt32(module.Assets.Value)).ToString());
                strUl = strUl.Replace("$Cash$", (Convert.ToInt32(module.Cash.Value)).ToString());
                int raceTimes = 0;
                if (module.RaceTimes.HasValue)
                {
                    raceTimes = module.RaceTimes.Value;
                }
                strUl = strUl.Replace("$RaceTimes$", raceTimes.ToString());
                strUl = strUl.Replace("$Rate$", "");
                strUl = strUl.Replace("$style$", "display:none");
                strUl = strUl.Replace("$StatusImg$", statusImg);
                strUl = strUl.Replace("$Alt$", strAlt);
                sbList.Append(strUl);
            }
            html = html.Replace("$ul$", sbList.ToString());
            html = html.Replace("$style$", "display:none");
            html = html.Replace("$Times$", "比赛次数");
            return html;
        }

        private string BindDetail(List<Jy123.Model.User_Module.User_TCenter> listTCenter)
        {
            string html = Cache["moduleranklist"].ToString();
            StringBuilder sbList = new StringBuilder();
            foreach (Jy123.Model.User_Module.User_TCenter module in listTCenter)
            {
                string strUl = Cache["modulerankul"].ToString();
                string statusImg = string.Empty;
                string strAlt = string.Empty;
                switch (module.TStatus)
                {
                    case 0:
                        statusImg = "/Images/Module/empty.gif";
                        strAlt = "空闲";
                        break;
                    case 1:
                        statusImg = "/Images/Module/training.gif";
                        strAlt = "等待";
                        break;
                    case 2:
                        statusImg = "/Images/Module/max.gif";
                        strAlt = "比赛";
                        break;
                    case 4:
                        statusImg = "/Images/Module/hire.gif";
                        strAlt = "租用";
                        break;
                }
                strUl = strUl.Replace("$ClubLogo$", module.Logo);
                string strCName = module.TName;
                if (strCName.Length > 5)
                {
                    strCName = strCName.Substring(0, 5) + "...";
                }
                strUl = strUl.Replace("$ClubName$", strCName);
                strUl = strUl.Replace("$ClubAssets$", (Convert.ToInt32(module.Assets.Value)).ToString());
                strUl = strUl.Replace("$Cash$", (Convert.ToInt32(module.Cash.Value)).ToString());
                
                strUl = strUl.Replace("$RaceTimes$", module.UsingTimes.ToString());
                strUl = strUl.Replace("$Rate$", "");
                strUl = strUl.Replace("$style$", "display:none");
                strUl = strUl.Replace("$StatusImg$", statusImg);
                strUl = strUl.Replace("$Alt$", strAlt);
                sbList.Append(strUl);
            }
            html = html.Replace("$ul$", sbList.ToString());
            html = html.Replace("$style$", "display:none");
            html = html.Replace("$Times$", "使用次数");
            return html;
        }
    }
}
